package com.example.quiz;

import Mis_clases.baseDatos;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.TabActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;

@SuppressWarnings("deprecation")
public class VentanaPuntuaciones extends TabActivity {

	private TabHost mTabHost;
	private baseDatos basedatos = new baseDatos(this);
	private SQLiteDatabase db;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ventana_puntuaciones);
        
        mTabHost = getTabHost();
        mTabHost.setup();
        
        TabSpec spec = mTabHost.newTabSpec("TAB_LOCAL");
        spec.setIndicator(null, getResources().getDrawable(R.drawable.ic_local));
        spec.setContent(R.id.tableLayout1);
        mTabHost.addTab(spec);
        
        spec = mTabHost.newTabSpec("TAB_AMIGOS");
        spec.setIndicator(null, getResources().getDrawable(R.drawable.ic_friends));
        spec.setContent(R.id.tableLayout2);
        mTabHost.addTab(spec);
        
        for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++){
        	mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
        }    
        
        mTabHost.setCurrentTabByTag("TAB_LOCAL");
        
        rellenaDatos();
    }

	private void rellenaDatos() {
		// TODO Auto-generated method stub
		String nombre;
		Integer puntuacion;
		TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
		
		db = basedatos.getReadableDatabase();
		if (db!=null){
			Cursor c = db.rawQuery("SELECT name, score FROM mytable", null);
			if (c.moveToFirst()){
				do{
					nombre = c.getString(0);
					puntuacion = c.getInt(1);
					
					TableRow row = new TableRow(this);
					TextView tvNombre = new TextView(this);
					TextView tvPuntuacion = new TextView(this);
					
					tvNombre.setText(nombre);
					tvPuntuacion.setText(puntuacion.toString());
					
					tvNombre.setGravity(android.view.Gravity.CENTER);
					tvPuntuacion.setGravity(android.view.Gravity.CENTER);
					
					tvNombre.setTypeface(null, Typeface.BOLD);
					tvPuntuacion.setTypeface(null, Typeface.BOLD);
					
					row.addView(tvNombre);
					row.addView(tvPuntuacion);
					
					table.addView(row);
					
				}while (c.moveToNext());
			}
		}
		
		db.close();
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	//Mostramos el submenu al pulsar la tecla menu
    	
    	MenuInflater inflater = getMenuInflater(); 
    	inflater.inflate(R.menu.activity_ventana_puntuaciones, menu);
    	
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	
    	switch(item.getItemId()){
    		case R.id.options2:
    			db = basedatos.getReadableDatabase();
    			db.execSQL("DELETE FROM mytable");
    			db.close();
    			
    			startActivity(getIntent());
    			finish();
    			return true;
    		default:
    			return true;	
    	}
    }
}
